/*
 * @Author: Xi Weidong
 * @Date: 2025-08-26 23:27:18
 * @LastEditors: Xi Weidong
 * @LastEditTime: 2025-08-26 23:40:08
 * @Description: 
 * @FilePath: \web\vite.config.ts
 */
import UnoCSS from 'unocss/vite'
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import path from "path";

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  //load env-list
  const env = loadEnv(mode, process.cwd(), "");
  return {
    base: env.VITE_APPLICATION_CONTEXT_PATH,
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      host: true,
      port: 11666,
      proxy: {
        "/api": {
          target: "http://localhost:11665/api-v1",
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ""),
        },
      },
    },
    plugins: [
      UnoCSS(),
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver({ importStyle: "sass" })],
      }),
    ],
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `
          @use "@/assets/styles/index.scss" as *;
        `,
        },
      },
    },
    build: {
      rollupOptions: {
        output: {
          // manualChunks: {
          //   vue: ['vue', 'vue-router', 'pinia'],
          //   elementPlus: ['element-plus'],
          //   echarts: ['echarts'],
          //   axios: ['axios'],
          //   '@': ['@']
          // }
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return 'vendor';
            }

            return null;
          }
        }
      }
    }
  };
});
